﻿
/****************************** 
 * 
 *  Create By: Arthur.li
 *  
 *  Create At: 2009年4月23日 13:40:43
 *  
 *  Description: 移动设备分配
 *  
 *  Remark: 
 *  
 * ****************************/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls.WebParts;
using System.Data;

using Jumpton.Data;
using Jumpton.SFA.Server.Entity;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.Persistent;


public partial class System_Mobile_UserDeviceInfo : PageBase
{
    //定义向叶面中的哪个Gridview中绑定数据
    enum BindingType
    {
        SIM,
        Mobile,
        User
    };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Initialization(true);
        }
        else
        {
            // 如果员工检索框中输入了默认值
            if (txtUser.Value.Equals(""))
            {
                BindSearch(BindingType.User,true);
            }
        }
    }

    /// <summary>
    /// 初始化页面上的所有数据
    /// </summary>
    private void Initialization(bool isInit)
    {
        BindData();
        BindSearch(BindingType.SIM, isInit);
        BindSearch(BindingType.Mobile, isInit);

        // 没做员工数据的绑定, 因为员工数据比较多,绑定太慢
        //BindSearch(BindingType.User);
    }

    protected void gvResult_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //DataRowView drv = (DataRowView)e.Row.DataItem;
            Button btnDelete = (Button)e.Row.FindControl("btnDelete");

            btnDelete.CommandName = "Delete";
            btnDelete.CommandArgument = ((UserDeviceInfo)e.Row.DataItem).ID.ToString();
            btnDelete.Attributes.Add("onclick", String.Format("return confirm(\"{0}\");",Resources.Mobile.SureDelete));
        }
    }

    protected void Search_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
    }

    /// <summary>
    /// 绑定移动设备分配
    /// </summary>
    protected void BindData()
    {
        ObjectTable ot = new UserDeviceInfoManager().LoadWithCondition("");

        gvResult.DataSource = ot;
        gvResult.DataBind();
      
    }

    /// <summary>
    ///  绑定检索结果
    /// </summary>
    /// <param name="type"></param>
    private void BindSearch(BindingType type,Boolean isInit)
    {
        ObjectTable searchResult = null;

        if (type == BindingType.Mobile)
        {
            if (isInit)
            {
                searchResult = new MobileDeviceInfoManager().QuickSearch(txtMobile.Value);
                FillMobile(searchResult);
            }
            else
            {
                ddlMobile.Items.Remove(ddlMobile.SelectedItem);
            }
        }
        else if (type == BindingType.SIM)
        {
            if (isInit)
            {
                searchResult = new SIMInfoManager().QuickSearch(txtSIM.Value);
                FillSIM(searchResult);
            }
            else
            {
                ddlSIM.Items.Remove(ddlSIM.SelectedItem);
            }
        }
        else
        {
            if (isInit)
            {
                //DataTable users = new EmployeeManager().QuickSearch(txtUser.Value);
                //FillUsers(users);
            }
            else
            {
                ddlUser.Items.Remove(ddlUser.SelectedItem);
            }
        }
    }

    /// <summary>
    /// 填充用户列表
    /// </summary>
    /// <param name="users"></param>
    private void FillUsers(DataTable users)
    {
        ddlUser.Items.Clear();
        ListItem item ;
        String itemFormat = "{0}; {1}; {2}";
        String itemText = "";

        foreach (DataRow row in users.Rows)
        {
            itemText = String.Format(itemFormat, row["code"].ToString().PadLeft(3, ' '), row["OrgName"].ToString(), row["PositionName"]);
            item = new ListItem(itemText,row["EmpId"].ToString());

            ddlUser.Items.Add(item);
        }
    }

    /// <summary>
    /// 填充SIM列表
    /// </summary>
    /// <param name="sims"></param>
    private void FillSIM(ObjectTable sims)
    {
        ddlSIM.Items.Clear();

        foreach (ObjectRow row in sims)
        {
            SIMInfo info = (SIMInfo)row;
            ddlSIM.Items.Add(new ListItem(info.PhoneNumber, info.ID.ToString()));
        }
    }

    /// <summary>
    /// 填充移动设备列表
    /// </summary>
    /// <param name="sims"></param>
    private void FillMobile(ObjectTable mobiles)
    {
        ddlMobile.Items.Clear();
        String itemFormat = Resources.Mobile.BrandName  +":{0}; " + Resources.Mobile.Model + ":{1}; IMEI:{2}";

        foreach (ObjectRow row in mobiles)
        {
            MobileDeviceInfo info = (MobileDeviceInfo)row;
            ddlMobile.Items.Add(new ListItem(String.Format(itemFormat,info.BrandName,info.Model,info.IMEICode),info.ID.ToString()));
        }
    }

    protected void btnSearchSIM_Click(object sender, EventArgs e)
    {
        BindSearch(BindingType.SIM, true);
    }

    protected void btnSearchMobile_Click(object sender, EventArgs e)
    {
        BindSearch(BindingType.Mobile, true);
    }

    protected void btnSearchUser_Click(object sender, EventArgs e)
    {
        BindSearch(BindingType.User, true);
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        //if (txtSimId.Value.Equals(String.Empty))
        if(String.IsNullOrEmpty(ddlSIM.SelectedValue))
        {
            return;
        }

        //if (txtMobileId.Value.Equals(String.Empty))
        if (String.IsNullOrEmpty(ddlMobile.SelectedValue))
        {
            return;
        }

        //if (txtUserId.Value.Equals(String.Empty))
        if (String.IsNullOrEmpty(ddlUser.SelectedValue))
        {
            return;
        }

        UserDeviceBound info = new UserDeviceBound();
        info.UserID = Decimal.Parse(ddlUser.SelectedValue);//Decimal.Parse(gvUser.DataKeys[int.Parse(txtUserId.Value)].Value.ToString());

        info.DeviceID = Decimal.Parse(ddlMobile.SelectedValue);//Decimal.Parse(gvMobile.DataKeys[int.Parse(txtMobileId.Value)].Value.ToString());
        
        info.ACTIVE = true;
        info.Save();

        // 重新绑定
        Initialization(false);
        BindSearch(BindingType.User,false);
    }

    protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Delete": // 删除一个移动资源分配
                {
                    DeleteUserDevice(Convert.ToDecimal(e.CommandArgument));
                    break;
                }
            default:
                break;
        }
    }

    /// <summary>
    /// 删除一个移动资源分配
    /// </summary>
    /// <param name="id"></param>
    private void DeleteUserDevice(Decimal id)
    {
        new UserDeviceBoundManager().Delete(id);

        // 重新绑定
        Initialization(true);
        BindSearch(BindingType.User,false);

        //ShowMessage(Resources.Mobile.DeleteUserDeviceSuccess);
    }

    protected void gvResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        // 这个方法必须的, 不要删除
    }

    protected void PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        gvResult.PageIndex = e.NewPageIndex;
        BindData();
    }
}
